/**
 * Created by qqlai on 2016/11/22.
 */
{
    angular.module('accommodationInquiryModule')
        .controller('studentInfoController',studentInfoController);
    studentInfoController.$inject = ['http.interfaces','$stateParams',"$scope"];
    function studentInfoController(interfaces,$stateParams,$scope) {
        let vm = this;
        let collect = {
            inputValue:'',
            toggle:false,
            info:{},
            active1:false,
            dateList:[],
            selectedDate:'',
            active2:false,
            information:false,
            recordsTtime:[],
            tab1:tab1,
            tab2:tab2,
            records:false,
            active: Active,
            getRecordsList:getRecordsList,
            dateChange:dateChange,
            search:search
        };
        collect.active();//进入控制器后调用一次
        vm.collect = collect;//输出集合，提供给模板调用
        vm.interfaces=interfaces;

        /** Active方法
         * @desc 控制器触发时执行一次的方法
         * @constructor
         */
        function Active() {
            $scope.$on('$ionicView.beforeEnter', function (event, data) {
                collect.search($stateParams.id);
            });
            for(var x=0;x<7;x++) {
                var time = new Date().getTime() - (x) * 24 * 60 * 60 * 1000;
                var year = new Date(time).getFullYear();
                var month = new Date(time).getMonth() + 1;
                var day = new Date(time).getDate();
                if(month<10) month="0"+month;
                if(day<10)　 day="0"+day;
                collect.dateList[x]=year+'-'+month+'-'+day;
            }
           
            collect.selectedDate=collect.dateList[0];
            
            
            getRecordsList();
        }
        function search(id) {
            interfaces.studentSearch({stuId: id}).then(res=> {
                var myData = res.data;
                if (myData.data.length == 0) {
                    alert("ERROR！请重新搜索")
                }else{
                    collect.info=myData.data;
                }
            });
        }

        function tab1() {
            collect.information=false;
            collect.active1=false;
            collect.active2=false;
            collect.records=false;
        }
        function tab2() {
            collect.information=true;
            collect.records=true;
            collect.active1=true;
            collect.active2=true;
        }
        
        function getRecordsList() {
            interfaces.records({stuId:$stateParams.id,search_Date:collect.selectedDate}).then(res=> {
                if(res.data.resultCode==1){
                    collect.recordsTtime=res.data.data;
                    console.log(res.data);
                }
            });
        }
        function dateChange() {
            collect.getRecordsList();
        }
    }
}